<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\BaseController;
use app\middleware\api\CheckLogin;
use app\validate\api\CheckId;
use think\Request;

class Product extends BaseController
{

    protected $middleware=[CheckLogin::class];

    /**
     * 全部商品
     * @param int $limit
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function index($limit=10)
    {
        return $this->success( \app\model\Product::with(['collect'=>function($query){
            $query->where('users_id',request()->userid);
        }])->paginate($limit));
    }

    /**
     * 热门产品
     * @return \think\response\Json
     */
    public function hotProducts($limit=10)
    {
        return $this->success(\app\model\Product::getHotProducts($limit,request()->userid));
    }

    /**
     * 产品详情
     * @return \think\response\Json
     * @throws \app\lib\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function detail()
    {
        (new CheckId())->goCheck();
        $id=request()->param('id');
        $data=['product_id'=>$id,'page_view'=>1,'sale'=>0,'num'=>0];
        event('SaleEvent',$data);
        return $this->success(\app\model\Product::where('id', $id)->with(['collect'])->find());
    }



}

